package cn.xxstudy.visitshop.ui.activity.login.viewmodel

import android.os.Looper
import android.util.Log
import androidx.lifecycle.MutableLiveData
import cn.xxstudy.visitshop.network.ApiService
import cn.xxstudy.visitshop.network.data.Response
import cn.xxstudy.visitshop.network.repository.BaseRepository
import cn.xxstudy.visitshop.ui.activity.login.model.LoginData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * @author ：DELL on 2021/3/23 .
 * @packages ：cn.xxstudy.visitshop.ui.activity.login.viewmodel .
 * TODO:登录
 */
class LoginRepository : BaseRepository() {
    suspend fun login(
        nickName: String,
        password: String,
        loginSuccess: MutableLiveData<LoginData>,
        loginFail: MutableLiveData<String>
    ) {
        val request = request<LoginData> { ApiService.api().login(nickName, password) }
        if (request is Response.SUCCESS) {
            //TODO 数据存储
            loginSuccess.postValue(request.data)
        }
        if (request is Response.FAILED) {
            loginFail.postValue(request.message)
        }
    }

    suspend fun login2(
        nickName: String,
        password: String
    ): Response<LoginData> {
        return withContext(Dispatchers.IO) {
            request { ApiService.api().login(nickName, password) }
        }
    }
}